/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfaceOpClientes.java
 *
 * Created on 04/07/2010, 16:50:56
 */

package imobiliaria.gui;

import imobiliaria.controladores.*;
import imobiliaria.entidades.*;
import imobiliaria.colecoes.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Thiago Ferreira
 */
public class InterfaceOpClientes extends javax.swing.JFrame implements InterfaceOpIF{
    private Sistema sis;
    /** Creates new form InterfaceOpClientes */
    public InterfaceOpClientes() {
        sis = new Sistema();
	try {
	    sis.atualizaDados();
	} catch (Exception e) {
	    System.out.println("Erro: " + e.getMessage());
	}
        initComponents();
        setLocationRelativeTo(null);
        atualiza();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        JP_VerInfo = new javax.swing.JPanel();
        JB_CadCliente = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        JList_Cliente = new javax.swing.JList();
        JB_DelCliente = new javax.swing.JButton();
        JB_Voltar = new javax.swing.JButton();
        JL_ProcuraNome = new javax.swing.JLabel();
        JTF_ProcuraNome = new javax.swing.JTextField();
        JB_Procura = new javax.swing.JButton();
        JP_AttCad = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        JList_ClienteInfo = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        JTF_Nome = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        JTF_Endereco = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        JTF_CpfCliente = new javax.swing.JTextField();
        JTF_Data = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        JTF_ProcuraNomeInf = new javax.swing.JTextField();
        JB_ProcuraInfo = new javax.swing.JButton();
        JB_SelectCliente = new javax.swing.JButton();
        JB_SalvaDados = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        JB_HistoCliente = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        JList_ClienteHist = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        JTF_ProcuraClienteIm = new javax.swing.JTextField();
        JB_ProcuraHist = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        JList_ImovelHist = new javax.swing.JList();
        JB_VerHistorico = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        JList_Pedido = new javax.swing.JList();
        jLabel9 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        JB_VerPedidos = new javax.swing.JButton();
        JTF_ProcuraNomePedido = new javax.swing.JTextField();
        JB_ProcuraPedido = new javax.swing.JButton();
        JB_SelectClientePed = new javax.swing.JButton();
        JCB_Pedido = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        JB_EfetuarPedido = new javax.swing.JButton();
        JB_CancelaPedido = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Opções Cliente");
        setResizable(false);

        JB_CadCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imobiliaria/images/addIcon.png"))); // NOI18N
        JB_CadCliente.setText("Cadastrar cliente");
        JB_CadCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_CadClienteActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de clientes"));

        JList_Cliente.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "< Você não possui nenhum cliente adicionado >" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(JList_Cliente);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
                .addContainerGap())
        );

        JB_DelCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imobiliaria/images/exitIcon.png"))); // NOI18N
        JB_DelCliente.setText("Excluir cadastro");
        JB_DelCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_DelClienteActionPerformed(evt);
            }
        });

        JB_Voltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imobiliaria/images/backIcon.png"))); // NOI18N
        JB_Voltar.setText("Voltar");
        JB_Voltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_VoltarActionPerformed(evt);
            }
        });

        JL_ProcuraNome.setText("Procura por nome:");

        JTF_ProcuraNome.setToolTipText("Digite o nome do cliente desejado");

        JB_Procura.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imobiliaria/images/procuraIcon.png"))); // NOI18N
        JB_Procura.setText("Procurar");
        JB_Procura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_ProcuraActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout JP_VerInfoLayout = new javax.swing.GroupLayout(JP_VerInfo);
        JP_VerInfo.setLayout(JP_VerInfoLayout);
        JP_VerInfoLayout.setHorizontalGroup(
            JP_VerInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_VerInfoLayout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .addGroup(JP_VerInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, JP_VerInfoLayout.createSequentialGroup()
                        .addGroup(JP_VerInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, JP_VerInfoLayout.createSequentialGroup()
                                .addComponent(JL_ProcuraNome)
                                .addGap(6, 6, 6)
                                .addComponent(JTF_ProcuraNome, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(JB_Procura))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, JP_VerInfoLayout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(JB_DelCliente)))
                        .addGap(77, 77, 77))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, JP_VerInfoLayout.createSequentialGroup()
                        .addComponent(JB_Voltar)
                        .addGap(18, 18, 18)
                        .addComponent(JB_CadCliente)
                        .addContainerGap())))
        );
        JP_VerInfoLayout.setVerticalGroup(
            JP_VerInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_VerInfoLayout.createSequentialGroup()
                .addGroup(JP_VerInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(JP_VerInfoLayout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(JP_VerInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(JL_ProcuraNome)
                            .addComponent(JTF_ProcuraNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(JB_Procura)))
                    .addGroup(JP_VerInfoLayout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(JB_DelCliente)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 135, Short.MAX_VALUE)
                .addGroup(JP_VerInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JB_Voltar)
                    .addComponent(JB_CadCliente))
                .addContainerGap())
        );

        JP_VerInfoLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {JB_CadCliente, JB_Voltar});

        jTabbedPane1.addTab("Listagem de cliente", JP_VerInfo);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de clientes"));

        JList_ClienteInfo.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "< Você não possui nenhum cliente adicionado >" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(JList_ClienteInfo);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane2)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Info. do cliente"));

        jLabel1.setText("Nome:");

        jLabel2.setText("Endereço:");

        jLabel3.setText("Cpf:");

        jLabel5.setText("Nascimento:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel5)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(JTF_Nome, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(JTF_Endereco))
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(JTF_Data, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(JTF_CpfCliente, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(JTF_Nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(JTF_Endereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(JTF_CpfCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(JTF_Data, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel7.setText("Procurar por nome:");

        JB_ProcuraInfo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imobiliaria/images/procuraIcon.png"))); // NOI18N
        JB_ProcuraInfo.setText("Procurar");
        JB_ProcuraInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_ProcuraInfoActionPerformed(evt);
            }
        });

        JB_SelectCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imobiliaria/images/okIcon.png"))); // NOI18N
        JB_SelectCliente.setText("Selecionar cliente");
        JB_SelectCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_SelectClienteActionPerformed(evt);
            }
        });

        JB_SalvaDados.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imobiliaria/images/saveIcon.png"))); // NOI18N
        JB_SalvaDados.setText("Salvar");
        JB_SalvaDados.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_SalvaDadosActionPerformed(evt);
            }
        });

        jLabel12.setText("Escolha um cliente");

        jLabel13.setText("e modifique seus");

        jLabel14.setText("dados, e salve");

        javax.swing.GroupLayout JP_AttCadLayout = new javax.swing.GroupLayout(JP_AttCad);
        JP_AttCad.setLayout(JP_AttCadLayout);
        JP_AttCadLayout.setHorizontalGroup(
            JP_AttCadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_AttCadLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(JP_AttCadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(JP_AttCadLayout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(JP_AttCadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel14)
                            .addGroup(JP_AttCadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel13)
                                .addGroup(JP_AttCadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel12)
                                    .addGroup(JP_AttCadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(JB_SalvaDados, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(JB_SelectCliente))))))
                    .addGroup(JP_AttCadLayout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(JTF_ProcuraNomeInf, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(JB_ProcuraInfo)))
                .addContainerGap())
            .addGroup(JP_AttCadLayout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(197, 197, 197))
        );

        JP_AttCadLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {JB_SalvaDados, JB_SelectCliente});

        JP_AttCadLayout.setVerticalGroup(
            JP_AttCadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, JP_AttCadLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addGroup(JP_AttCadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(JTF_ProcuraNomeInf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(JB_ProcuraInfo))
                .addGap(26, 26, 26)
                .addGroup(JP_AttCadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(JP_AttCadLayout.createSequentialGroup()
                        .addComponent(jLabel12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel14)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                        .addComponent(JB_SelectCliente)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(JB_SalvaDados))
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        JP_AttCadLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {JB_SalvaDados, JB_SelectCliente});

        jTabbedPane1.addTab("Informações do cliente", JP_AttCad);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de clientes"));

        JList_ClienteHist.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "< Você não possui nenhum cliente adicionado >" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(JList_ClienteHist);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jLabel4.setText("Procurar por nome:");

        JB_ProcuraHist.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imobiliaria/images/procuraIcon.png"))); // NOI18N
        JB_ProcuraHist.setText("Procurar");
        JB_ProcuraHist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_ProcuraHistActionPerformed(evt);
            }
        });

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Historico de Imoveis"));

        JList_ImovelHist.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "< Não possui nenhum imóvel adicionado >" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane4.setViewportView(JList_ImovelHist);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                .addContainerGap())
        );

        JB_VerHistorico.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imobiliaria/images/homeIcon.png"))); // NOI18N
        JB_VerHistorico.setText("Ver historico");
        JB_VerHistorico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_VerHistoricoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout JB_HistoClienteLayout = new javax.swing.GroupLayout(JB_HistoCliente);
        JB_HistoCliente.setLayout(JB_HistoClienteLayout);
        JB_HistoClienteLayout.setHorizontalGroup(
            JB_HistoClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JB_HistoClienteLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(JB_HistoClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, JB_HistoClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, JB_HistoClienteLayout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addGap(18, 18, 18)
                            .addComponent(JTF_ProcuraClienteIm, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(JB_ProcuraHist))))
                .addGap(18, 18, 18)
                .addComponent(JB_VerHistorico)
                .addGap(77, 77, 77))
        );
        JB_HistoClienteLayout.setVerticalGroup(
            JB_HistoClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JB_HistoClienteLayout.createSequentialGroup()
                .addGroup(JB_HistoClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(JB_HistoClienteLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(JB_HistoClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(JTF_ProcuraClienteIm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(JB_ProcuraHist))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(JB_HistoClienteLayout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(JB_VerHistorico)))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Historico de cliente", JB_HistoCliente);

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de clientes"));

        JList_Pedido.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "< Você não possui nenhum cliente adicionado >" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane5.setViewportView(JList_Pedido);

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 592, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel9.setText("Procurar por nome:");

        jLabel6.setText("Selecione um  cliente");

        jLabel8.setText("e veja os seus pedidos:");

        JB_VerPedidos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imobiliaria/images/homeIcon.png"))); // NOI18N
        JB_VerPedidos.setText("Ver pedidos");
        JB_VerPedidos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_VerPedidosActionPerformed(evt);
            }
        });

        JB_ProcuraPedido.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imobiliaria/images/procuraIcon.png"))); // NOI18N
        JB_ProcuraPedido.setText("Procurar");
        JB_ProcuraPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_ProcuraPedidoActionPerformed(evt);
            }
        });

        JB_SelectClientePed.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imobiliaria/images/okIcon.png"))); // NOI18N
        JB_SelectClientePed.setText("Selecionar cliente");
        JB_SelectClientePed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_SelectClientePedActionPerformed(evt);
            }
        });

        JCB_Pedido.setMaximumRowCount(5);

        jLabel10.setText("Pedidos:");

        JB_EfetuarPedido.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imobiliaria/images/okIcon.png"))); // NOI18N
        JB_EfetuarPedido.setText("Efetuar");
        JB_EfetuarPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_EfetuarPedidoActionPerformed(evt);
            }
        });

        JB_CancelaPedido.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imobiliaria/images/exitIcon.png"))); // NOI18N
        JB_CancelaPedido.setText("Cancelar");
        JB_CancelaPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_CancelaPedidoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel6Layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(JB_SelectClientePed)
                                        .addGroup(jPanel6Layout.createSequentialGroup()
                                            .addComponent(JTF_ProcuraNomePedido, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(JB_ProcuraPedido)))
                                    .addGroup(jPanel6Layout.createSequentialGroup()
                                        .addGap(35, 35, 35)
                                        .addComponent(JB_EfetuarPedido)
                                        .addGap(18, 18, 18)
                                        .addComponent(JB_CancelaPedido)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel6)
                                    .addGroup(jPanel6Layout.createSequentialGroup()
                                        .addGap(12, 12, 12)
                                        .addComponent(JB_VerPedidos)))
                                .addGap(47, 47, 47))
                            .addGroup(jPanel6Layout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(JCB_Pedido, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())))))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(2, 2, 2)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(JB_VerPedidos))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(JTF_ProcuraNomePedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(JB_ProcuraPedido))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(JB_SelectClientePed)))
                .addGap(36, 36, 36)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(JCB_Pedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JB_CancelaPedido)
                    .addComponent(JB_EfetuarPedido))
                .addContainerGap(68, Short.MAX_VALUE))
        );

        jPanel6Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {JB_CancelaPedido, JB_EfetuarPedido});

        jTabbedPane1.addTab("Pedidos", jPanel6);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 668, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents



    /*
     * --- ABA: Listagem de clientes
     */
    private void JB_CadClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_CadClienteActionPerformed
        new CadastraCliente().setVisible(true);
    }//GEN-LAST:event_JB_CadClienteActionPerformed

    private void JB_ProcuraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_ProcuraActionPerformed
        atualiza();
    }//GEN-LAST:event_JB_ProcuraActionPerformed

    private void JB_VoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_VoltarActionPerformed
        dispose();
    }//GEN-LAST:event_JB_VoltarActionPerformed

    private void JB_DelClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_DelClienteActionPerformed
        
        boolean clienteRemovidoOK = false;
        try{
            String nomeCpf = JList_Cliente.getSelectedValue().toString();
            String soCpf = nomeCpf.substring(nomeCpf.lastIndexOf(" "), nomeCpf.length());
            Cliente clienteRemove = ControladorCliente.getInstance().getCliente(soCpf.trim());
            ControladorCliente.getInstance().removeCliente(clienteRemove.getCpf());
            ControladorLogin.getInstance().removeLogin( clienteRemove.getLogin().getUserName());
            sis.salvarDados();
            clienteRemovidoOK = true;

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Erro ao tentar remover cliente",
                    "Erro Ao Remover Cliente", JOptionPane.ERROR_MESSAGE);
        }

        if(clienteRemovidoOK){
            atualiza();
        }

    }//GEN-LAST:event_JB_DelClienteActionPerformed
    // --- Fim de ABA: Listagem de clientes


    /*
     * --- ABA: Informacoes do cliente
     */
    private void JB_VerHistoricoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_VerHistoricoActionPerformed
        // VER HISTORICO
        try{
            String nomeCpf = JList_ClienteHist.getSelectedValue().toString();
            String soCpf = nomeCpf.substring(nomeCpf.lastIndexOf(" "), nomeCpf.length());
            final Cliente clienteHistorico = ControladorCliente.getInstance().getCliente(soCpf.trim());

            JList_ImovelHist.setModel(new javax.swing.AbstractListModel() {

                String[] strings = mostraImoveis(clienteHistorico);

                public int getSize() {
                    return strings.length;
                }

                public Object getElementAt(int i) {
                    return strings[i];
                }
            });
            

        }catch (Exception ex){
            JOptionPane.showMessageDialog(null, " Cliente Inválido\nVocê deve escolher um cliente",
                    "Erro Ao Escolher Cliente", JOptionPane.ERROR_MESSAGE);
        }


    }//GEN-LAST:event_JB_VerHistoricoActionPerformed
    private Imovel[] mostraPedidos(Cliente cliente){
        Imovel[] pedidos = ControladorPedidos.getInstance().listaDePedidosGUI(cliente.getCpf());
        return pedidos;
    }
    
    private String[] mostraImoveis(Cliente cliente){
        String[] imoveis;
        ColecaoImoveis historicoCliente = cliente.getHistoricoCompras();

        // Se nao tiver imoveis, deixar mensagem default
        if( historicoCliente.getImoveis().isEmpty()){
            imoveis = new String[1];
            imoveis[0] = "< Não possui nenhum imóvel adicionado >";
            return imoveis;
        }

        imoveis = new String[historicoCliente.getImoveis().size()];
        for (int i = 0; i < historicoCliente.getImoveis().size(); i++) {
            imoveis[i] = historicoCliente.getImoveis().get(i).toString();
        }

        return imoveis;
    }

    private void JB_ProcuraHistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_ProcuraHistActionPerformed
        atualiza();
    }//GEN-LAST:event_JB_ProcuraHistActionPerformed

    private void JB_VerPedidosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_VerPedidosActionPerformed
        try{
            String nomeCpf = JList_ClienteInfo.getSelectedValue().toString();
            String soCpf = nomeCpf.substring(nomeCpf.lastIndexOf(" "), nomeCpf.length());
            new InterfaceListagem("Pedidos", "Listagem de Pedidos do Cliente", ControladorPedidos.getInstance()
                    .listaPedidosDeCliente(soCpf)).setVisible(true);
        }catch (Exception ex){
            JOptionPane.showMessageDialog(null, " Cliente Inválido",
                    "Erro Ao Escolher Cliente", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_JB_VerPedidosActionPerformed

    private void JB_SalvaDadosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_SalvaDadosActionPerformed
        if (JList_ClienteInfo.getSelectedValue() == null){
        	return;
        }
    	String nomeCpf = JList_ClienteInfo.getSelectedValue().toString();
        String soCpf = nomeCpf.substring(nomeCpf.lastIndexOf(" "), nomeCpf.length());
        boolean clienteModificadoOK = false;
        Calendar data;

        try{
            Cliente clienteMod = ControladorCliente.getInstance().getCliente(soCpf.trim());
            clienteMod.setNome(JTF_Nome.getText());
            clienteMod.setEndereco(JTF_Endereco.getText());
            
            String cpfSemPonto = JTF_CpfCliente.getText().replace(".", "");
            String cpf = cpfSemPonto.replace("-", "");
            clienteMod.setCpf(cpf);

            String[] nascimento = JTF_Data.getText().split("/");
            data = new GregorianCalendar(Integer.parseInt(nascimento[2]),
                    Integer.parseInt(nascimento[1]) - 1,
                    Integer.parseInt(nascimento[0]));
            clienteMod.setDataNascimento(data);



            sis.salvarDados();
            clienteModificadoOK = true;

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Erro ao salvar dados",
                    "ERRO", JOptionPane.ERROR_MESSAGE);
        }

        if(clienteModificadoOK){
            atualiza();
        }

    }//GEN-LAST:event_JB_SalvaDadosActionPerformed

    private void JB_SelectClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_SelectClienteActionPerformed
        try{
            String nomeCpf = JList_ClienteInfo.getSelectedValue().toString();
            String soCpf = nomeCpf.substring(nomeCpf.lastIndexOf(" "), nomeCpf.length());
            Cliente clienteMod = ControladorCliente.getInstance().getCliente(soCpf.trim());
            JTF_Nome.setText(clienteMod.getNome());
            JTF_Endereco.setText(clienteMod.getEndereco());
            JTF_CpfCliente.setText(clienteMod.getCpf());
            JTF_Data.setText(clienteMod.getDataNascimento());
            
        }catch (Exception ex){
            JOptionPane.showMessageDialog(null, " Cliente Inválido\nEscolha um cliente na lista",
                    "Erro Ao Escolher Cliente", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_JB_SelectClienteActionPerformed

    private void JB_ProcuraInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_ProcuraInfoActionPerformed
        atualiza();
}//GEN-LAST:event_JB_ProcuraInfoActionPerformed

    private void JB_ProcuraPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_ProcuraPedidoActionPerformed
        atualiza();
    }//GEN-LAST:event_JB_ProcuraPedidoActionPerformed

    private void JB_SelectClientePedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_SelectClientePedActionPerformed
        try{
            String nomeCpf = JList_Pedido.getSelectedValue().toString();
            String soCpf = nomeCpf.substring(nomeCpf.lastIndexOf(" "), nomeCpf.length());
            Cliente clientePedi = ControladorCliente.getInstance().getCliente(soCpf.trim());

            Imovel[] pedidos = ControladorPedidos.getInstance().listaDePedidosGUI(clientePedi.getCpf());
            System.out.print(pedidos.length);

        }catch (Exception ex){
            JOptionPane.showMessageDialog(null, " Cliente Inválido",
                    "Erro Ao Escolher Cliente", JOptionPane.ERROR_MESSAGE);
        }

        
    }//GEN-LAST:event_JB_SelectClientePedActionPerformed

    private void JB_EfetuarPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_EfetuarPedidoActionPerformed
        if (JCB_Pedido.getSelectedItem() == null) {
            JOptionPane.showMessageDialog(null, "Nenhum Pedido Selecionado",
                    "Efetuar Pedido", JOptionPane.ERROR_MESSAGE);
        }

        Pedido pedido = (Pedido) JCB_Pedido.getSelectedItem();

        new InterfaceRecebeCreci(pedido, this).setVisible(true);
    }//GEN-LAST:event_JB_EfetuarPedidoActionPerformed

    private void JB_CancelaPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_CancelaPedidoActionPerformed
        if (JCB_Pedido.getSelectedItem() == null) {
            JOptionPane.showMessageDialog(null, "Nenhum Pedido Selecionado",
                    "Pedido", JOptionPane.ERROR_MESSAGE);
            return;
        }

        try {
        ControladorPedidos.getInstance().removePedido(
                ((Pedido) JCB_Pedido.getSelectedItem()).getImovel()
                .getRegistroImovel() + "");
        sis.salvarDados();
        atualiza();
            JOptionPane.showMessageDialog(null, "Pedido Removido com Sucesso",
                    "Pedido", JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Erro ao excluir Pedido:\n" +
                    e.getMessage(), "Pedido", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_JB_CancelaPedidoActionPerformed
    
    /*
     * Metodo de Busca por nomes
     */
    private String[] mostraClientes(String nomeProcurado) {
        TreeSet<Cliente> conjCliente = ControladorCliente.getInstance().getColecaoClientes().getClientes();
        ArrayList<String> clientesProcurados = new ArrayList<String>();
        String[] clientes;

        // Se nao tiver clientes, deixar mensagem default
        if( conjCliente.isEmpty()){
            clientes = new String[1];
            clientes[0] = "< Você não possui nenhum cliente adicionado >";
            return clientes;
        }

        // Procura pela entrada do usuario (sem case sensitive)
        for (Cliente cliente : conjCliente) {
            if (cliente.getNome().toLowerCase().contains(nomeProcurado.toLowerCase())) {
                    clientesProcurados.add(cliente.getNome() + " - CPF: " + cliente.getCpf());
            }
        }
        // Adiciona no array que ira ser usado no JList
        clientes = new String[clientesProcurados.size()];

        if (clientes.length == 0){
            clientes = new String[1];
            clientes[0] = "< Nenhum cliente encontrado >";
            return clientes;
        }

        for (int i = 0; i < clientesProcurados.size(); i++) {
		clientes[i] = clientesProcurados.get(i);
	}
	return clientes;
    }

    private void preencheCBox() {

        JCB_Pedido.removeAllItems();
        for (Pedido p : ControladorPedidos.getInstance().getListaPedidos())
            JCB_Pedido.addItem(p);
    }

    public void atualiza(){
        preencheCBox();
        final String nome = JTF_ProcuraNome.getText();
        final String nomeInfo = JTF_ProcuraNomeInf.getText();
        final String nomeHist = JTF_ProcuraClienteIm.getText();
        final String nomePed = JTF_ProcuraNomePedido.getText();

        //mostraClientes(nome);
        JList_Cliente.setModel(new javax.swing.AbstractListModel() {
            String[] strings = mostraClientes(nome);
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });

        JList_ClienteInfo.setModel(new javax.swing.AbstractListModel() {
            String[] strings = mostraClientes(nomeInfo);
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });

        JList_ClienteHist.setModel(new javax.swing.AbstractListModel() {
            String[] strings = mostraClientes(nomeHist);
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });

        JList_Pedido.setModel(new javax.swing.AbstractListModel() {
            String[] strings = mostraClientes(nomePed);
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
    }


   
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InterfaceOpClientes().setVisible(true);
                
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JB_CadCliente;
    private javax.swing.JButton JB_CancelaPedido;
    private javax.swing.JButton JB_DelCliente;
    private javax.swing.JButton JB_EfetuarPedido;
    private javax.swing.JPanel JB_HistoCliente;
    private javax.swing.JButton JB_Procura;
    private javax.swing.JButton JB_ProcuraHist;
    private javax.swing.JButton JB_ProcuraInfo;
    private javax.swing.JButton JB_ProcuraPedido;
    private javax.swing.JButton JB_SalvaDados;
    private javax.swing.JButton JB_SelectCliente;
    private javax.swing.JButton JB_SelectClientePed;
    private javax.swing.JButton JB_VerHistorico;
    private javax.swing.JButton JB_VerPedidos;
    private javax.swing.JButton JB_Voltar;
    private javax.swing.JComboBox JCB_Pedido;
    private javax.swing.JLabel JL_ProcuraNome;
    private javax.swing.JList JList_Cliente;
    private javax.swing.JList JList_ClienteHist;
    private javax.swing.JList JList_ClienteInfo;
    private javax.swing.JList JList_ImovelHist;
    private javax.swing.JList JList_Pedido;
    private javax.swing.JPanel JP_AttCad;
    private javax.swing.JPanel JP_VerInfo;
    private javax.swing.JTextField JTF_CpfCliente;
    private javax.swing.JTextField JTF_Data;
    private javax.swing.JTextField JTF_Endereco;
    private javax.swing.JTextField JTF_Nome;
    private javax.swing.JTextField JTF_ProcuraClienteIm;
    private javax.swing.JTextField JTF_ProcuraNome;
    private javax.swing.JTextField JTF_ProcuraNomeInf;
    private javax.swing.JTextField JTF_ProcuraNomePedido;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables

}
